<?php 
    App::uses('my_image', 'Vendor');
    class ImageHelper extends AppHelper {

        var $helpers = array('Html'); 
        var $cacheDir = 'cache'; // relative to IMAGES_URL path 
        public function make_thumb($src,$dest,$desired_width, $desired_height)
        {
            $image= WWW_ROOT."/img/".$src;
            $mImage= new my_image($image);
            $mImage->thumbnail($desired_width, $desired_height, 0);
            $ext = strrchr($src, ".");
            $pos = strrpos($src, ".");
            $filename = substr($src,0,$pos);
            $folder = basename(dirname($filename));
            $filename = basename($filename) . '_thumbs' . $ext;
            if (!file_exists(WWW_ROOT . '/img/uploads/thumbnails/' . $folder)) {
                mkdir(WWW_ROOT . '/img/uploads/thumbnails/' . $folder, 0777, true);
            }
            //$thumbnail_name = $filename."_thumb".$ext;
            $prevName = '/uploads/thumbnails/' . $folder . '/' . $filename;
            $thumbnail_name = WWW_ROOT . '/img/' . $prevName;
            //$mImage->copyTo($dest.$thumbnail_name);
            $mImage->copyTo($thumbnail_name);
            return $prevName;
        }
        //custom $htmlAttributes;
        function resize($path, $dst_w, $dst_h, $htmlAttributes = array(), $return = false) {
            $types = array(1 => "gif", "jpeg", "png", "swf", "psd", "wbmp"); // used to determine image type 

            $fullpath = ROOT.DS.APP_DIR.DS.WEBROOT_DIR.DS.IMAGES_URL; 

            $url = $fullpath.$path; 

            list($w, $h, $type) = getimagesize($url);
            $r = $w / $h;
            $dst_r = $dst_w / $dst_h;

            if ($r > $dst_r) {
                $src_w = $h * $dst_r;
                $src_h = $h;
                $src_x = ($w - $src_w) / 2;
                $src_y = 0;
            } else {
                $src_w = $w;
                $src_h = $w / $dst_r;
                $src_x = 0;
                $src_y = ($h - $src_h) / 2;
            }

            $relfile = $this->cacheDir.'/'.$dst_w.'x'.$dst_h.'_'.basename($path); // relative file 
            $cachefile = $fullpath.$relfile;

            if (file_exists($cachefile)) {
                if (@filemtime($cachefile) >= @filemtime($url)) {
                    $cached = true;
                } else {
                    $cached = false;
                }
            } else { 
                $cached = false; 
            } 

            if (!$cached) { 
                $image = call_user_func('imagecreatefrom'.$types[$type], $url); 
                if (function_exists("imagecreatetruecolor")) {
                    $temp = imagecreatetruecolor($dst_w, $dst_h); 
                    imagecopyresampled($temp, $image, 0, 0, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h); 
                } else { 
                    $temp = imagecreate ($dst_w, $dst_h); 
                    imagecopyresized($temp, $image, 0, 0, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h); 
                } 
                call_user_func("image".$types[$type], $temp, $cachefile); 
                imagedestroy($image); 
                imagedestroy($temp); 
            }

            return $this->Html->image($relfile, $htmlAttributes);
        }
    }
?>